package com.haiyu.manager.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.haiyu.manager.dao.Category1Mapper;
import com.haiyu.manager.dao.Category2Mapper;
import com.haiyu.manager.dto.Category1DTO;
import com.haiyu.manager.dto.Category2DTO;
import com.haiyu.manager.dto.Category2SearchDTO;
import com.haiyu.manager.pojo.Category1;
import com.haiyu.manager.pojo.Category2;
import com.haiyu.manager.response.PageDataResult;
import com.haiyu.manager.service.Category2Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class Category2ServiceImpl implements Category2Service {
    @Autowired
    private Category2Mapper category2Mapper;
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    Category1Mapper category1Mapper;

    @Override
    public PageDataResult getCategory2List(Category2SearchDTO category2Search, Integer pageNum, Integer pageSize) {
        PageDataResult pageDataResult = new PageDataResult();
        List<Category2DTO> category2s = category2Mapper.getCategory2List(category2Search);

        System.out.println("category2s有"+category2s);
        PageHelper.startPage(pageNum, pageSize);

        if(category2s.size() != 0){
            PageInfo<Category2DTO> pageInfo = new PageInfo<>(category2s);
            pageDataResult.setList(category2s);
            pageDataResult.setTotals((int) pageInfo.getTotal());
        }

        return pageDataResult;
    }

    @Override
    public Map<String, Object> addCategory2(Category2 category2) {
        Map<String,Object> data = new HashMap();
        try {
            Category2 old = category2Mapper.getCategory2ByName(category2.getCategory2Name(),null);
            if(old != null){
                data.put("code",0);
                data.put("msg","类别名已存在！");
                logger.error("类别名[新增]，结果=类别名已存在！");
                return data;
            }

            if(category2.getCategory2Remark() == null){
                String category2Remark = "无";
                category2.setCategory2Remark(category2Remark);
            }
            int result = category2Mapper.insert(category2);
            if(result == 0){
                data.put("code",0);
                data.put("msg","新增失败！");
                logger.error("category2[新增]，结果=新增失败！");
                return data;
            }
            data.put("code",1);
            data.put("msg","新增成功！");
            logger.info("category2[新增]，结果=新增成功！");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("category2[新增]异常！", e);
            return data;
        }
        return data;
    }

    @Override
    public Map<String, Object> updateCategory2(Category2 category2) {
        Map<String,Object> data = new HashMap();
        Integer category2Id = category2.getCategory2Id();

        Category1 mun = category1Mapper.getCategory1(category2.getCategory10Id());
        System.out.println("mun="+  mun);
        Category2 old = category2Mapper.getCategory2ByName(category2.getCategory2Name(),category2Id);
        if(old != null){
            data.put("code",0);
            data.put("msg","category2已存在！");
            logger.error("category2[更新]，结果=category2已存在！");
            return data;
        }

        if(mun==null){
            //System.out.println("........"+category2Mapper.getCategory1By2Id(category2Id).get(0));
            data.put("code", 0);
            data.put("msg", "编辑category2失败,其所属分类已失效！");

        }else {

            int result = category2Mapper.updateCategory2(category2);
            if (result == 0) {
                data.put("code", 0);
                data.put("msg", "更新失败！");
                logger.error("category2[更新]，结果=更新失败！");
                return data;
            }
            data.put("code", 1);
            data.put("msg", "更新成功！");
            logger.info("category2[更新]，结果=更新成功！");
        }
        return data;
    }

    @Override
    public Category2 getCategory2ById(Integer category2Id) {
        return category2Mapper.selectByPrimaryKey(category2Id);
    }

    @Override
    public Category2 findByCategory2Name(String category2Name) {
        return  category2Mapper.findByCategory2Name(category2Name);
    }

    @Override
    public Map<String, Object> delCategory2(Integer category2Id, Integer category2Label) {
        Map<String, Object> data = new HashMap<>();
        try {
            // 删除用户
            int result = category2Mapper.updateCategory2Status( category2Id,  category2Label);
            if(result == 0){
                data.put("code",0);
                data.put("msg","删除category2失败");
                logger.error("删除category2失败");
                return data;
            }
            data.put("code",1);
            data.put("msg","删除category2成功");
            logger.info("删除category2成功");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("删除category2异常！", e);
        }
        return data;
    }

    @Override
    public Map<String, Object> recoverCategory2(Integer category2Id, Integer category2Label) {
        Map<String, Object> data = new HashMap<>();
        try {
            List<Category1DTO> category1s = category2Mapper.getCategory1By2Id(category2Id);
            System.out.println(category1s);
            if(category2Mapper.getCategory1By2Id(category2Id).size()==0){
                data.put("code", 0);
                data.put("msg", "恢复category2失败,其所属分类已失效！");
            }else {
                int result = category2Mapper.updateCategory2Status(category2Id, category2Label);
                if (result == 0) {
                    data.put("code", 0);
                    data.put("msg", "恢复category2失败！");
                }
                data.put("code", 1);
                data.put("msg", "恢复category2成功！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("恢复category2异常！", e);
        }
        return data;
    }
}
